package com.site.web.controller.war;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.site.common.utils.RSAUtils;
import com.site.war.domain.SiteFaceRecord;
import com.site.war.domain.SiteFiveLimitWarning;
import com.site.war.service.ISiteFaceRecordService;
import com.site.war.service.ISiteFiveLimitWarningService;
import com.site.war.domain.SiteVideo;
import com.site.war.domain.SiteWarning;
import com.site.war.service.ISiteVideoService;
import com.site.war.service.ISiteWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Title: SiteWarAPIConrtroller
 * @Author LinLuoChen
 * @ProjectName SiteWar
 * @Description: 用于对接第三方使用
 * @Date 2023/9/22 16:52
 */
@RestController
@RequestMapping("/war/api")
public class SiteWarAPIController {

    private static String publieKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmB8zcsYEbl9Yo+upDuPN2y1++/weJ+vZhBS8L2H/H" +
            "HPG1q6KWPV7zOrC7EhZ6dHB0RGq9rTyiZb+b1AKzfxsqf5pOmBOoK36EahU0hDR9zDVKPbfGaWLuEbVllyMQ32+nlQRxC3z2451cwQIAnKm" +
            "7FsbLHj7qgXIjIKgTViMDGQIDAQAB";

    @Autowired
    private ISiteWarningService siteWarningService;

    @Autowired
    private ISiteVideoService siteVideoService;

    @Autowired
    private ISiteFaceRecordService siteFaceRecordService;

    @Autowired
    private ISiteFiveLimitWarningService siteFiveLimitWarningService;

    /**
     * @Description 接收塔吊预警信息
     * @Author LinLuoChen
     * @Date  2023/9/22 16:56
     **/
    @ResponseBody
    @RequestMapping(value = "receiveSiteWarning",method = {RequestMethod.POST,RequestMethod.GET})
    public JSONObject receiveSiteWarning(@RequestBody JSONObject json) throws Exception{
        // 判断当前 JSON 不为 NULL
        if (!json.isEmpty()){
            String sign = json.containsKey("sign") ? json.getString("sign") : "";
            String data = json.containsKey("data") ? json.getString("data") : "";
            // 判断签名是否正确
            if(RSAUtils.verify(data, RSAUtils.getPublicKey(publieKey), sign)){
                String decryptData = RSAUtils.decrypt(data);
                JSONArray array = JSONArray.parse(decryptData);
                if(array.size() > 0){
                    // 循环写入数据
                    for (int i = 0; i < array.size(); i++) {
                        SiteWarning sw = new SiteWarning();
                        sw.setSiteNumber(array.getJSONObject(i).containsKey("siteNumber") ? array.getJSONObject(i).getString("siteNumber") : "");
                        sw.setSiteName(array.getJSONObject(i).containsKey("siteName") ? array.getJSONObject(i).getString("siteName") : "");
                        sw.setTowerCraneNumber(array.getJSONObject(i).containsKey("towerCraneNumber") ? array.getJSONObject(i).getString("towerCraneNumber") : "");
                        sw.setWarningPosition(array.getJSONObject(i).containsKey("warningPosition") ? array.getJSONObject(i).getString("warningPosition") : "");
                        sw.setWarningType(array.getJSONObject(i).containsKey("warningType") ? array.getJSONObject(i).getString("warningType") : "");
                        sw.setWarningContent(array.getJSONObject(i).containsKey("warningContent") ? array.getJSONObject(i).getString("warningContent") : "");
                        sw.setWarningTime(array.getJSONObject(i).containsKey("warningTime") ? array.getJSONObject(i).getDate("warningTime") : null);
                        sw.setWarningStatic(array.getJSONObject(i).containsKey("warningStatic") ? array.getJSONObject(i).getLong("warningStatic") : null);
                        sw.setDeviceType(array.getJSONObject(i).containsKey("deviceType") ? array.getJSONObject(i).getLong("deviceType") : null);
                        sw.setPushTime(array.getJSONObject(i).containsKey("pushTime") ? array.getJSONObject(i).getDate("pushTime") : null);
                        siteWarningService.insertSiteWarning(sw);
                    }
                    return receiveJson("200","推送成功","1");
                }
            }
            return receiveJson("1002","sign 签名错误","0");
        }
        return receiveJson("1000","JSON 数据为空","0");
    }

    /**
     * @Description 接收塔吊视频信息
     * @Author LinLuoChen
     * @Date  2023/9/22 16:56
     **/
    @ResponseBody
    @RequestMapping(value = "receiveSiteVideo",method = {RequestMethod.POST,RequestMethod.GET})
    public JSONObject receiveSiteVideo(@RequestBody JSONObject json) throws Exception{
        // 判断当前 JSON 不为 NULL
        if (!json.isEmpty()){
            String sign = json.containsKey("sign") ? json.getString("sign") : "";
            String data = json.containsKey("data") ? json.getString("data") : "";
            // 判断签名是否正确
            if(RSAUtils.verify(data, RSAUtils.getPublicKey(publieKey), sign)){
                String decryptData = RSAUtils.decrypt(data);
                JSONArray array = JSONArray.parse(decryptData);
                if(array.size() > 0){
                    // 循环写入数据
                    for (int i = 0; i < array.size(); i++) {
                        SiteVideo sv = new SiteVideo();
                        sv.setSiteNumber(array.getJSONObject(i).containsKey("siteNumber") ? array.getJSONObject(i).getString("siteNumber") : "");
                        sv.setSiteName(array.getJSONObject(i).containsKey("siteName") ? array.getJSONObject(i).getString("siteName") : "");
                        sv.setTowerCraneNumber(array.getJSONObject(i).containsKey("towerCraneNumber") ? array.getJSONObject(i).getString("towerCraneNumber") : "");
                        sv.setVideoName(array.getJSONObject(i).containsKey("videoName") ? array.getJSONObject(i).getString("videoName") : "");
                        sv.setRealtimeVideoUrl(array.getJSONObject(i).containsKey("realtimeVideoUrl") ? array.getJSONObject(i).getString("realtimeVideoUrl") : "");
                        sv.setPlaybackVideoUrl(array.getJSONObject(i).containsKey("playbackVideoUrl") ? array.getJSONObject(i).getString("playbackVideoUrl") : "");
                        sv.setDeviceType(array.getJSONObject(i).containsKey("deviceType") ? array.getJSONObject(i).getLong("deviceType") : null);
                        sv.setToken(array.getJSONObject(i).containsKey("token") ? array.getJSONObject(i).getString("token") : null);
                        sv.setPushTime(array.getJSONObject(i).containsKey("pushTime") ? array.getJSONObject(i).getDate("pushTime") : null);
                        siteVideoService.insertSiteVideo(sv);
                    }
                    return receiveJson("200","推送成功","1");
                }
            }
            return receiveJson("1002","sign 签名错误","0");
        }
        return receiveJson("1000","JSON 数据为空","0");
    }


    /**
     * @Description 接收人脸识别记录信息
     * @Author ZYJ
     * @Date  2023年10月13日16:06:17
     **/
    @ResponseBody
    @RequestMapping(value = "receiveSiteFace",method = {RequestMethod.POST,RequestMethod.GET})
    public JSONObject receiveSiteFace(@RequestBody JSONObject json) throws Exception{
        // 判断当前 JSON 不为 NULL
        if (!json.isEmpty()){
            String sign = json.containsKey("sign") ? json.getString("sign") : "";
            String data = json.containsKey("data") ? json.getString("data") : "";
            // 判断签名是否正确
            if(RSAUtils.verify(data, RSAUtils.getPublicKey(publieKey), sign)){
                String decryptData = RSAUtils.decrypt(data);
                JSONArray array = JSONArray.parse(decryptData);
                if(array.size() > 0){
                    // 循环写入数据
                    for (int i = 0; i < array.size(); i++) {
                        SiteFaceRecord sf = new SiteFaceRecord();
                        sf.setPersonId(array.getJSONObject(i).containsKey("personId") ? array.getJSONObject(i).getLong("personId") : null);
                        sf.setPersonName(array.getJSONObject(i).containsKey("personName") ? array.getJSONObject(i).getString("personName") : "");
                        sf.setPersonJob(array.getJSONObject(i).containsKey("personJob") ? array.getJSONObject(i).getString("personJob") : "");
                        sf.setTowerCraneNumber(array.getJSONObject(i).containsKey("towerCraneNumber") ? array.getJSONObject(i).getString("towerCraneNumber") : "");
                        sf.setFaceTime(array.getJSONObject(i).containsKey("faceTime") ? array.getJSONObject(i).getDate("faceTime") : null);
                        siteFaceRecordService.insertSiteFaceRecord(sf);
                    }
                    return receiveJson("200","推送成功","1");
                }
            }
            return receiveJson("1002","sign 签名错误","0");
        }
        return receiveJson("1000","JSON 数据为空","0");
    }


    /**
     * @Description 接收五限位预警信息
     * @Author ZYJ
     * @Date  2023年10月13日16:06:17
     **/
    @ResponseBody
    @RequestMapping(value = "receiveSiteFive",method = {RequestMethod.POST,RequestMethod.GET})
    public JSONObject receiveSiteFive(@RequestBody JSONObject json) throws Exception{
        // 判断当前 JSON 不为 NULL
        if (!json.isEmpty()){
            String sign = json.containsKey("sign") ? json.getString("sign") : "";
            String data = json.containsKey("data") ? json.getString("data") : "";
            // 判断签名是否正确
            if(RSAUtils.verify(data, RSAUtils.getPublicKey(publieKey), sign)){
                String decryptData = RSAUtils.decrypt(data);
                JSONArray array = JSONArray.parse(decryptData);
                if(array.size() > 0){
                    // 循环写入数据
                    for (int i = 0; i < array.size(); i++) {
                        SiteFiveLimitWarning sf = new SiteFiveLimitWarning();
                        sf.setDeviceSn(array.getJSONObject(i).containsKey("deviceSn") ? array.getJSONObject(i).getString("deviceSn") : "");
                        sf.setDeviceId(array.getJSONObject(i).containsKey("deviceId") ? array.getJSONObject(i).getLong("deviceId") : null);
                        sf.setDeviceAuthCode(array.getJSONObject(i).containsKey("deviceAuthCode") ? array.getJSONObject(i).getString("deviceAuthCode") : "");
                        sf.setMonitorDateTime(array.getJSONObject(i).containsKey("monitorDateTime") ? array.getJSONObject(i).getDate("monitorDateTime") : null);
                        sf.setName(array.getJSONObject(i).containsKey("name") ? array.getJSONObject(i).getString("name") : "");
                        sf.setIdNo(array.getJSONObject(i).containsKey("idNo") ? array.getJSONObject(i).getString("idNo") : "");
                        sf.setMultiple(array.getJSONObject(i).containsKey("multiple") ? array.getJSONObject(i).getString("multiple") : "");
                        sf.setMoment(array.getJSONObject(i).containsKey("moment") ? array.getJSONObject(i).getString("moment") : null);
                        sf.setWeight(array.getJSONObject(i).containsKey("weight") ? array.getJSONObject(i).getString("weight") : null);
                        sf.setWindSpeed(array.getJSONObject(i).containsKey("windSpeed") ? array.getJSONObject(i).getString("windSpeed") : null);
                        sf.setWindLevel(array.getJSONObject(i).containsKey("windLevel") ? array.getJSONObject(i).getString("windLevel") : null);
                        sf.setRrange(array.getJSONObject(i).containsKey("rrange") ? array.getJSONObject(i).getString("rrange") : null);
                        sf.setHeight(array.getJSONObject(i).containsKey("height") ? array.getJSONObject(i).getString("height") : null);
                        sf.setAngle(array.getJSONObject(i).containsKey("angle") ? array.getJSONObject(i).getString("angle") : null);
                        sf.setObliguity(array.getJSONObject(i).containsKey("obliguity") ? array.getJSONObject(i).getString("obliguity") : null);
                        siteFiveLimitWarningService.insertSiteFiveLimitWarning(sf);
                    }
                    return receiveJson("200","推送成功","1");
                }
            }
            return receiveJson("1002","sign 签名错误","0");
        }
        return receiveJson("1000","JSON 数据为空","0");
    }


    // 通用返回参数
    private JSONObject receiveJson(String code,String msg,Object data){
        JSONObject json = new JSONObject();
        json.put("code",code);
        json.put("msg",msg);
        json.put("data",data);
        return json;
    }


}
